const crypto= require("crypto")
const keys = 'wsnd'


// 加密函数 aes data 需要加密的字段
function aesEncrypt(data,key){
    key = key || keys
    const cipher = crypto.createCipher('aes192',key)
    var crypted = cipher.update(data,'utf8','hex')
    crypted += cipher.final('hex')
    return crypted
}



//解密
function aesDecrypt(encrypted,key){
    key = key ||keys
    const decipher = crypto.createDecipher('aes192',key)
    var decrypted = decipher.update(encrypted,'hex','utf8')
    decrypted += decipher.final('utf8')
    // console.log(decipher);
    return decrypted //明文
}



exports.aesEncrypt = aesEncrypt;//加密
exports.aesDecrypt = aesDecrypt; //解密
exports.keys = keys; //密钥



exports.checkSession = function(req,res,callback){
    if(req.session.username){
        callback(req.session.username)
      }else{
        res.send(`<script>alert('session失效，重新登录');location.href='/login'</script>`)
      }
}